<template>
  <div class="shop">
    <p class="shopdetail">采购单明细</p>

    <div class="detailheader">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="purchaseId" label="采购单号" />
        <el-table-column prop="supervisorName" label="制单人" />
        <el-table-column prop="address" label="收货地址" />
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column prop="eliveryTime" label="供货日期" />
        <el-table-column prop="status" label="采购状态">
          <template slot-scope="scope">
            {{ scope.row.status | statusFitlter }}
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="btn">
      <el-button type="primary" class="account" @click="createDelivery()"
        >生成送货单</el-button
      >
    </div>

    <div class="table">
      <el-table
        :data="datadetail"
        border
        style="width: 100%"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column type="index" width="50" label="序号" />
        <el-table-column prop="goodsId" label="商品ID" />
        <el-table-column prop="goodsName" label="商品名称" />
        <el-table-column prop="num" label="采购数量" />
        <el-table-column prop="unit" label="辅助单位" />
        <el-table-column prop="weight" label="采购重量(kg)" />
        <el-table-column prop="price" label="采购单价" />
        <el-table-column prop="totalPrice" label="总金额" />
      </el-table>
    </div>
  </div>
</template>

<script>
import { getshopdetail, createDelivery } from "@/api/partner.js";
export default {
  filters: {
    statusFitlter(value) {
      if (value === 1) {
        return "发出";
      } else if (value === 2) {
        return "生成";
      }
    }
  },
  data() {
    return {
      tableData: [],
      datadetail: []
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let purchaseId = { purchaseId: this.$route.query.purchaseId };
      getshopdetail(purchaseId).then(res => {
        console.log(res);
        if (res.code === 20000) {
          var arr = [];
          arr.push(res.data);
          this.tableData = arr;
          this.datadetail = res.data.purchaseGoods;
        }
      });
    },
    // 生成送货单
    createDelivery() {
      let purchaseId = { purchaseId: this.$route.query.purchaseId };
      createDelivery(purchaseId).then(res => {
        console.log(res);
        if (res.code === 20000) {
          this.$message({
            message: "生成成功",
            type: "success"
          });
          this.$router.push({
            path: "deliverydetail",
            query: { deliveryId: res.data }
          });
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //   合计列
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      var allmoney = 0;
      var goodsNum = 0;
      var weight = 0
      data.map(item => {
        allmoney += Number(item.totalPrice);
        goodsNum += Number(item.num);
        weight += Number(item.weight);
        return { allmoney, goodsNum,weight };
      });
      columns.forEach((column, index) => {
        sums[2] = "合计数量";
        sums[3] = goodsNum.toFixed(2);

        sums[4] = "合计重量";
        sums[5] = weight.toFixed(2);

        sums[6] = "合计金额";
        sums[7] = allmoney.toFixed(2);
      });

      return sums;
    }
  }
};
</script>

<style lang="scss" scoped>
.shop {
  padding: 20px 20px;
  .shopdetail {
    font-size: 22px;
    margin-bottom: 60px;
  }
  .btn {
    text-align: right;
    margin: 30px;
  }
}
</style>
